package com.neusoft.neumooc.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.neusoft.core.common.BaseDAO;
import com.neusoft.core.common.exception.DAOException;
import com.neusoft.neumooc.VO.OrderVO;


/** 
 * @ClassName: OrderDAOImpl 
 * @Description: TODO 订单管理业务数据访问类
 * @author zls
 * @date 2016年11月1日
 */
@Repository
public class OrderDAOImpl extends BaseDAO implements OrderDAO{
	
	/** 
	 * @Fields GET_ORDRELIST_SQL : TODO 获得订单列表
	 */ 
	private final static String GET_ORDRELIST_SQL = "SELECT wemall_order.id,wemall_user.username AS userName,wemall_user.phone,wemall_user.address,CONCAT(wemall_user.username,'|',wemall_user.phone,'|',wemall_user.address)AS user,wemall_order.orderid AS orderId,wemall_order.totalprice AS totalPrice,wemall_order.pay_style AS payStyle,wemall_order.pay_status AS payStatus,wemall_order.note,wemall_order.order_status AS orderStatus,wemall_order.time,wemall_order.cartdata AS cartData FROM wemall_order LEFT JOIN wemall_user ON wemall_order.user_id = wemall_user.uid";
	/** 
	 * @Fields GET_ORDERLIST_NUMBER_SQL : TODO 获得订单列表计数
	 */ 
	private final static String GET_ORDERLIST_NUMBER_SQL = "SELECT COUNT(1) FROM wemall_order";
	
	/** 
	 * @Fields UPDATE_PAY_STATUS_SQL : TODO 更改订单支付状态
	 */ 
	private final static String UPDATE_PAY_STATUS_SQL = "UPDATE wemall_order SET pay_status = 1 WHERE orderid = ?";
	
	/** 
	 * @Fields UPDATE_OREDR_STATUS_SQL : TODO 更改订单发货状态
	 */ 
	private final static String UPDATE_OREDR_STATUS_SQL = "UPDATE wemall_order SET order_status = 1 WHERE orderid = ?";
	
	/** 
	 * @Fields DELETE_ORDER_SQL : TODO 删除订单
	 */ 
	private final static String DELETE_ORDER_SQL =  "DELETE FROM wemall_order WHERE orderid = ?";
	
	/** 
	 * @Fields INSERT_ORDER_SQL : TODO 增加订单
	 */ 
	
	private final static String INSERT_ORDER_SQL = "INSERT INTO wemall_order(id,user_id,orderid,totalprice,pay_style,pay_status,note,order_status,time,cartdata) VALUES(:id,:userId,:orderId,:totalPrice,:payStyle,:payStatus,:note,:orderStatus,NOW(),:cartData)";
	
	
	
	
	/** 
	 * @Title: getOrderList 
	 * @Description: TODO 获得订单列表
	 * @param pageIndex 起始页数
	 * @param pageSize 页面大小
	 * @return List
	 * @throws DAOException
	 */
	@Override
	public List<OrderVO> getOrderList(int pageIndex, int pageSize)
			throws DAOException {
		return this.getPageData(GET_ORDRELIST_SQL, OrderVO.class, pageIndex, pageSize);
	}

	
	/** 
	 * @Title: getOrderNum 
	 * @Description: TODO 获得订单列表计数
	 * @return int
	 * @throws DAOException
	 */
	@Override
	public int getOrderNum() throws DAOException {
		return this.queryForInteger(GET_ORDERLIST_NUMBER_SQL);
	}

	
	/** 
	 * @Title: changeOrderPay 
	 * @Description: TODO 更改订单支付状态
	 * @param orderId 订单ID
	 * @return int
	 * @throws DAOException
	 */
	@Override
	public int changeOrderPay(String orderId) throws DAOException {
		Object[] args = { orderId };
		return this.update(UPDATE_PAY_STATUS_SQL, args);
	}


	/** 
	 * @Title: changeOrderStatus 
	 * @Description: TODO 更改订单发货状态
	 * @param orderId 订单ID
	 * @return int
	 * @throws DAOException
	 */
	@Override
	public int changeOrderStatus(String orderId) throws DAOException {
		Object[] args = { orderId };
		return this.update(UPDATE_OREDR_STATUS_SQL, args);
	}

	
	/** 
	 * @Title: deleteOrder 
	 * @Description: TODO 删除订单
	 * @param orderId 订单ID
	 * @return int
	 * @throws DAOException
	 */
	@Override
	public int deleteOrder(String orderId) throws DAOException {
		Object[] args = { orderId };
		return this.update(DELETE_ORDER_SQL, args);
	}


	
	/** 
	 * @Title: addOrder 
	 * @Description: TODO 增加订单
	 * @param ordervo 订单实体
	 * @return int
	 * @throws DAOException
	 */
	@Override
	public int addOrder(OrderVO orderVO) throws DAOException {
		return this.update(INSERT_ORDER_SQL, orderVO);
	}

}
